package com.situ.mydictionary;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.toolbox.Loger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static com.situ.mydictionary.R.id.tran_information;
import static com.situ.mydictionary.R.id.tran_input;
import static com.situ.mydictionary.R.id.view_offset_helper;

public class DictionaryActivity extends AppCompatActivity {
    //输入框
    private EditText dic_input;
    //搜索按钮
    private Button dic_search;
    //基本信息布局
    private LinearLayout dic_information;
    //要查询的内容
    private TextView dic_contain;
    //发音按钮
    private Button dic_music;
    //音标
    private TextView dic_yinbiao;
    //释义
    private TextView dic_mean;
    private MediaPlayer mediaPlayer; //mediaplayer

    private Button b1, b2, b3;
    public void player() throws IOException {
        String word = dic_input.getText().toString().trim();
        final String contain = "http://dict.youdao.com/dictvoice?audio=" + word;

        mediaPlayer.setDataSource(this, Uri.parse(contain));
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.reset();
            }
        });
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dictionary_layout);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        this.b1 = (Button) findViewById(R.id.translation);
        this.b2 = (Button) findViewById(R.id.dictionary);
        this.b3 = (Button) findViewById(R.id.index);

        dic_input = (EditText) findViewById(R.id.dic_input);
        dic_search = (Button) findViewById(R.id.dic_search);
        dic_information = (LinearLayout) findViewById(R.id.dic_information);
        dic_contain = (TextView) findViewById(R.id.dic_contain);
        dic_music = (Button) findViewById(R.id.dic_music);
        dic_yinbiao = (TextView) findViewById(R.id.dic_yinbiao);
        dic_mean = (TextView) findViewById(R.id.dic_mean);


        //查询之前基本信息不可见
        dic_information.setVisibility(View.GONE);

        b1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(DictionaryActivity.this, TranslateActivity.class);
                startActivity(intent);
                finish();
            }
        });

        b3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(DictionaryActivity.this, Main_Activity.class);
                startActivity(intent);
                finish();
            }
        });
        dic_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = dic_input.getText().toString().trim();

                try {
                    word = URLEncoder.encode(word, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                if (null != word && !"".equals(word)) {
                    String contans = "http://v.juhe.cn/xhzd/query?key=2dfdd9bc5b46fc8b6aa7bdfd682dd80d&word=" + word;

                    //执行网络请求
                    RxVolley.get(contans, new HttpCallback() {
                        @Override
                        public void onSuccess(String t) {
                            Loger.debug("请求的数据：" + t);
                            parseJson(t);
                        }

                        private void parseJson(String json) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                String basic = null;
                                String phonetic = null;
                                String interpre = null;

                                if (jsonObject.getInt("error_code") == 0) {
                                    if (jsonObject.has("result")) {
                                        JSONObject resultObject = jsonObject.getJSONObject("result");

                                        if(resultObject.has("zi")) {
                                            basic = resultObject.getString("zi");
                                        }

                                        if(resultObject.has("pinyin")) {
                                            phonetic = resultObject.getString("pinyin");
                                        }

                                        if (resultObject.has("jijie")) {
                                            JSONArray jijieArr = resultObject.getJSONArray("jijie");
                                            interpre = jijieArr.getString(2);
                                            for (int i = 3; i < jijieArr.length()-4; i ++) {
                                                interpre += "\n" + jijieArr.getString(i);
                                            }
                                        }
                                    }
                                }

                                dic_information.setVisibility(View.VISIBLE);
                                //清空信息
                                dic_contain.setText("");
                                dic_yinbiao.setText("");
                                dic_mean.setText("");

                                dic_contain.setText(basic);
                                dic_yinbiao.setText(phonetic);
                                dic_mean.setText(interpre);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });

        dic_music.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Toast.makeText(DictionaryActivity.this, "中华文化博大精深，怎么会有准确的发音呢，绝不是我没找到发音接口！", Toast.LENGTH_LONG).show();
                try {
                    player();
                } catch (IOException e) {
//
                }

            }
        });
    }
    }